package com.example.Controller;

import com.example.entity.Goods;
import com.example.entity.Orderdetail;
import com.example.entity.Orders;
import com.example.service.OrderdetailService;
import com.example.service.impl.GoodsServiceImpl;
import com.example.service.impl.OrderdetailServiceImpl;
import com.example.service.impl.OrdersServiceImpl;
import com.github.pagehelper.PageInfo;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.yaml.snakeyaml.events.Event;

import javax.annotation.Resource;
import java.util.ArrayList;

@Controller
@RequestMapping("/admin")
public class OrderdetailCtrl {
    @Resource
    OrderdetailServiceImpl orderdetailService;
    @Resource
    GoodsServiceImpl goodsService;

//    @RequestMapping (value = "/orderdetail" ,method = {RequestMethod.GET , RequestMethod.POST})
//    public String findAllOrderdetail(Model model, @RequestParam(value = "pageNum",required = false, defaultValue = "1") Integer pageNum
//            , @RequestParam(value = "pageSize",required = false, defaultValue = "4") Integer pageSize){
//        PageInfo<Orderdetail> orderdetailList =  orderdetailService.queryAll(pageNum,pageSize);
//
//        model.addAttribute("orderdetailList",orderdetailList);
//        return "orderdetailList";
//    }

    @RequestMapping (value = "/selorderdetail/{id}")
    public String findAllOrderdetail(Model model, @PathVariable("id") int id){
        ArrayList<Orderdetail> list = orderdetailService.query(id);
        model.addAttribute("orderdetailList",list);
        model.addAttribute("id",id);


        return "orderdetailList";
    }


    @GetMapping("/delOrderdetail/{id}")
    public String delOrderdetail(@PathVariable("id") int id, Model model){
        int e = orderdetailService.deleteByID(id);
        model.addAttribute("msg","删除成功");
        return "forward:/admin/orderdetail";
    }
    @GetMapping(value = "/orderdetail/{id}")
    public String getOrderdetailById(@PathVariable("id") int id,Model model){
        Orderdetail orderdetail = orderdetailService.findById(id);
        ArrayList<Goods> list = goodsService.query();
        model.addAttribute("orderdetail",orderdetail);
        model.addAttribute("goods",list);
        return "orderdetailEdit";
    }
    @PostMapping("/editOrderdetail")
    public String addOrderdetail(Orderdetail orderdetail,Model model){
        int w = orderdetailService.update(orderdetail);
        model.addAttribute("msg","商品信息更新成功！");
        ArrayList<Goods> goodsList = goodsService.query();
        model.addAttribute("goodsList",goodsList);
        return "forward:/admin/selorderdetail/"+orderdetail.getOdid();
    }
    @GetMapping("/gotoAddOrderdetail")
    public String gotoAddorderdetail(Model model,Integer oID){
        ArrayList<Goods> goodsList = goodsService.query();
        model.addAttribute("goodsList",goodsList);
        model.addAttribute("oID",oID);
        return "orderdetailAdd";
    }
    @PostMapping("/addOrderdetail")
    public String addOrderdetail(Orderdetail orderdetail){
        int q = orderdetailService.insert(orderdetail);
        return "forward:/admin/selorderdetail/"+orderdetail.getOdid();
    }
    @GetMapping("/toorderdetail")
    public String toorderdetail(){
        return "orderdetailList";
    }
}
